package javaapplication1.dao.oracleImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javaapplication1.model.Account;
import javaapplication1.dao.AccountDao;

public class AccountDaoOracleImpl implements AccountDao {

	private Connection connection = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;

	@Override
	public boolean createAccount(Account account) {
		boolean flag = false;
		try {
			String sql = "insert into T_ACCOUNT values(SEQ_T_ACCOUNT.nextval,?,?,?,?,?,?)";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			ps.setInt(1, account.getDealType());
			ps.setInt(2, account.getPetId());
			ps.setInt(3, account.getSellerId());
			ps.setInt(4, account.getBuyerId());
			ps.setInt(5, account.getPrice());
			ps.setDate(6, new java.sql.Date(account.getDealTime().getTime()));
			int line = ps.executeUpdate();
			flag = (line == 1) ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return flag;
	}

	@Override
	public boolean deleteAccount(int id) {
		boolean flag = false;
		try {
			String sql = "delete from T_ACCOUNT where id = ?";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			ps.setInt(1, id);
			int line = ps.executeUpdate();
			flag = (line == 1) ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return flag;
	}

	@Override
	public boolean updateAccount(Account account) {
		return false;
	}

	@Override
	public Account retrieveAccount(int id) {
		Account account = null;
		try {
			String sql = "select * from T_ACCOUNT where id = ?";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				account = new Account();
				account.setId(rs.getInt("ID"));
				account.setDealType(rs.getInt("DEAL_TYPE"));
				account.setPetId(rs.getInt("PET_ID"));
				account.setSellerId(rs.getInt("SELLER_ID"));
				account.setBuyerId(rs.getInt("BUYER_ID"));
				account.setPrice(rs.getInt("PRICE"));
				account.setDealTime(rs.getDate("DEAL_TIME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return account;
	}

	@Override
	public List<Account> retrieveAccountAll() {
		List<Account> accountList = new ArrayList<Account>();
		try {
			String sql = "select * from T_ACCOUNT";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				Account account = new Account();
				account = new Account();
				account.setId(rs.getInt("ID"));
				account.setDealType(rs.getInt("DEAL_TYPE"));
				account.setPetId(rs.getInt("PET_ID"));
				account.setSellerId(rs.getInt("SELLER_ID"));
				account.setBuyerId(rs.getInt("BUYER_ID"));
				account.setPrice(rs.getInt("PRICE"));
				account.setDealTime(rs.getDate("DEAL_TIME"));
				accountList.add(account);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return accountList;
	}

}